package com.bitejiuyeke.job_publish.common;

import com.bitejiuyeke.job_publish.utils.StringUtils;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.springframework.stereotype.Component;

/**
 * 分页请求
 *
 * @Author 比特就业课
 */
@Data
@Component
public class PageRequest {

    @JsonIgnore
    private Integer pageNum; // 当前页号
    @JsonIgnore
    private Integer pageSize; // 每页记录数
    @JsonIgnore
    private Integer offset; // 偏移量
    @JsonIgnore
    private String sort = "updateTime"; // 排序字段
    @JsonIgnore
    private String order = "DESC"; // 排序类型

    public void setOrder (String order) {
        // 默认排序类型为DESC
        if ("DESC".equalsIgnoreCase(order) || "ASC".equalsIgnoreCase(order)) {
            this.order = order;
        }
    }

    public void setSort (String sort) {
        if (!StringUtils.isEmpty(sort)) {
            this.sort = sort;
        }
    }

    public Integer getOffset () {
        return (pageNum - 1) * pageSize;
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        if (pageNum < 1) {
            this.pageNum = 1;
            return;
        }
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        if (pageSize < 1) {
            this.pageSize = 1;
            return;
        }
        this.pageSize = pageSize;
    }

    public void setOffset(Integer offset) {
        this.offset = offset;
    }

    public String getSort() {
        return sort;
    }

    public String getOrder() {
        return order;
    }
}
